package net.ncraft.mc.ezframework.core.handler.eboard;

import lombok.Getter;
import lombok.Setter;
import net.ncraft.mc.ezframework.core.SpigotEzFrameworkCore;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scoreboard.Criteria;
import org.bukkit.scoreboard.DisplaySlot;
import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.Scoreboard;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.UUID;

public class EBoard implements Listener {

    private static final Random rd = new Random();
    private static final String[] COLORS = {"§a", "§b", "§c", "§d"};
    @Getter
    private Player player;
    private Scoreboard bukkitScoreboard;
    private Objective objective;
    @Getter
    @Setter
    private String title;
    @Getter
    private List<String> lines = new ArrayList<>();
    @Getter
    @Setter
    private ERender render;
    @Getter
    BukkitRunnable task;

    public EBoard(String title) {
        this.title = title;
        bukkitScoreboard = Bukkit.getScoreboardManager().getNewScoreboard();
        objective = bukkitScoreboard.registerNewObjective(UUID.randomUUID().toString(), Criteria.DUMMY, title);
        objective.setDisplaySlot(DisplaySlot.SIDEBAR);
        objective.setDisplayName(title);
    }

    public void init() {
        Bukkit.getServer().getPluginManager().registerEvents(this, SpigotEzFrameworkCore.getPlugin());
    }

    public void reset() {
        for (Objective bukkitScoreboardObjective : bukkitScoreboard.getObjectives()) {
            bukkitScoreboardObjective.unregister();
        }
        objective = bukkitScoreboard.registerNewObjective(UUID.randomUUID().toString(), Criteria.DUMMY, title);
        objective.setDisplaySlot(DisplaySlot.SIDEBAR);
        objective.setDisplayName(title);
    }

    public void show(Player player) {
        this.player = player;
        init();
        player.setScoreboard(bukkitScoreboard);
        render();
        task = new BukkitRunnable() {
            @Override
            public void run() {
                flush();
            }
        };
        task.runTaskTimer(SpigotEzFrameworkCore.getPlugin(), 0, 5);
    }

    public void hide() {
        task.cancel();
        player.setScoreboard(Bukkit.getScoreboardManager().getMainScoreboard());
    }

    @EventHandler
    public void hide(PlayerQuitEvent event) {
        if (!event.getPlayer().getUniqueId().toString().equals(player.getUniqueId().toString())) {
            return;
        }
        hide();
    }


    public void render() {
        List<String> newLines = render.render(this);
        boolean changed = false;
        if (newLines.size() != lines.size()) {
            changed = true;
        } else {
            for (int i = 0; i < lines.size(); i++) {
                String text = lines.get(i);
                if (!newLines.get(i).equals(text)) {
                    changed = true;
                    break;
                }
            }
        }
        if (changed) {
            lines = newLines;
        } else {
            return;
        }

        reset();

        for (int i = 0; i < lines.size(); i++) {
            String text = lines.get(i);
            text += COLORS[rd.nextInt(COLORS.length)];
            objective.getScore(text).setScore(lines.size() - i);
        }
    }

    public void flush() {
        if (render != null) {
            render();
        }
    }
}
